﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetTop10WaitStatsTableAdapters;
using SQLServerOptimizer.PL.WPF.Datasets.DataSetPerformanceDashboardTableAdapters;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SQLServerOptimizer.PL.WPF.Datasets;
using Microsoft.Reporting.WinForms;
using System.Configuration;
using SQLServerHealthCheck.Model;
using System.Windows.Forms;
using SWC = System.Windows.Controls;
using AG.Utilities;

namespace SQLServerOptimizer.PL.WPF
{
	/// <summary>
	/// Interaction logic for WaitstatsDashboard.xaml
	/// </summary>
    public partial class WaitstatsDashboard : SWC.UserControl
    {
        public WaitstatsDashboard()
        {
            this.InitializeComponent();
        }


        public void RefreshReport(Trace trace)
        {

            try
            {

                ReportViewer viewer = (ReportViewer)wfh_rptTop10WaitStats.Child;

                PerformanceDashboardTableAdapter adapter = new PerformanceDashboardTableAdapter();
                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetPerformanceDashboard dataset = new DataSetPerformanceDashboard();
                adapter.Fill(dataset.PerformanceDashboard, trace.TraceID);


                BindingSource bindingSource = new BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "PerformanceDashboard";


                ReportDataSource reportDataSource = new ReportDataSource();
                viewer.Reset();
                reportDataSource.Name = "DataSetPerformanceDashboard";
                reportDataSource.Value = bindingSource;


                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\WaitstatDashboard.rdlc";
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = BorderStyle.None;

                viewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
                viewer.RefreshReport();



            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }





        }
        void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {

            int serverID = Convert.ToInt32(e.Parameters["ServerID"].Values[0]);
            int traceID = Convert.ToInt32(e.Parameters["TraceID"].Values[0]);
            if (e.DataSourceNames[0] == "DataSetTop10WaitStats")
            {


                Top10WaitStatsTableAdapter adapter = new Top10WaitStatsTableAdapter();

                DataSetTop10WaitStats dataset = new DataSetTop10WaitStats();

                adapter.Fill(dataset.Top10WaitStats, serverID, traceID);

                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "Top10WaitStats";
                ReportDataSource reportDataSource = new ReportDataSource();

                reportDataSource.Name = "DataSetTop10WaitStats";
                reportDataSource.Value = bindingSource;
                e.DataSources.Add(reportDataSource);
            }
        }
    }
}